package com.first.server.controller.admin;

import com.first.common.constant.JwtClaimsConstant;
import com.first.common.context.BaseContext;
import com.first.common.properties.JwtProperties;
import com.first.common.result.PageResult;
import com.first.common.result.Result;

import com.first.common.utils.TokenUtil;
import com.first.pojo.dto.AdminLoginDTO;
import com.first.pojo.dto.UserDTO;
import com.first.pojo.dto.UserPageQueryDTO;
import com.first.pojo.entity.Admin;
import com.first.pojo.entity.UserPlace;
import com.first.pojo.vo.AdminLoginVO;
import com.first.pojo.vo.UserPageQueryVO;
import com.first.server.service.UserPlaceService;
import com.first.server.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.security.auth.login.AccountNotFoundException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@RestController("adminUserController")
@Api(tags = "管理员管理接口")
@RequestMapping("admin/user")
public class UserController {
    @Autowired
    private UserService userService;
    @Autowired
    private UserPlaceService userPlaceService;

    /**
     * 登录
     *
     * @param adminLoginDTO
     * @return
     */
    @GetMapping("/login")
    @ApiOperation("管理员登录")
    public Result<String> login(@RequestBody AdminLoginDTO adminLoginDTO) throws AccountNotFoundException {
        log.info("员工登录：{}", adminLoginDTO);
        Admin admin = userService.login(adminLoginDTO);

        if (admin != null) {
            String res = TokenUtil.sign(admin.getName(), String.valueOf(admin.getId()), String.valueOf(admin.getPlaceId()));
            return Result.success(res);
        }
        return Result.success("失败");
    }

    /**
     * 退出
     *
     * @return
     */
    @PostMapping("/logout")
    @ApiOperation("登出")
    public Result<String> logout() {
        return Result.success();
    }

    @GetMapping("/page")
    @ApiOperation("分页查询")
    public Result<PageResult> page(UserPageQueryVO userPageQueryVO) {
        log.info("分页查询：{}",userPageQueryVO);
        PageResult pageResult = userService.pageQuery(userPageQueryVO);
        return Result.success(pageResult);
    }




//    @GetMapping("/adminbase")
//    @ApiOperation("获取线程信息")
//    public Result<Long> getAdmin(){
//         Long id= BaseContext.getCurrentId();
//         Long placeId= BaseContext.getCurrentPlaceId();
//        System.out.println("placeId:"+placeId);
//         log.info("登录信息:{}", id);
//         return Result.success(id);
//    }
}
